﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Users</title>
    <link href="~/lib/layui/css/layui.css" rel="stylesheet" />
     <script src="~/lib/layui/layui.js"></script>
    <script src="~/js/jquery.min.js"></script>
</head>
<body>
    <table class="layui-hide" id="demo" lay-filter="test"></table>
    <script id="toolbarDemo" type="text/html">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-normal" lay-event="add">添加</button>
        </div>
    </script>
    <script id="barDemo" type="text/html">
        <a class="layui-btn layui-btn-xs" lay-event="Search">角色</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs" lay-event="RePwoard">重置密码</a>
    </script>
    <div id="yh_role" style="display:none" class="layui-elem-field layui-field-title">
        <div id="role_transfer" class="demo-transfer"></div>
    </div>
    <script>
        layui.use(['table', 'layer', 'form', 'laydate', 'transfer'], function () {
            $ = layui.jquery;
            var layer = layui.layer, laydate = layui.laydate, table = layui.table, form = layui.form, transfer = layui.transfer;

            var tableIns = table.render({
                elem: '#demo'
                , height: 420
                , url: '/System/Useres' //数据接口
                , title: '用户表'
                , page: true //开启分页
                , toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                , totalRow: true //开启合计行,
                , id: 'testReload'
                , cols: [[ //表头
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'ZhangHao', title: '账号', width: 80 }
                    , { field: 'XingMin', title: '用户名', width: 100, sort: true }
                    , { field: 'XiuGaiSJTime', title: '修改时间', width: 200 },
                    { field: 'DengLuTime', title: '登陆时间', width: 200 }
                    , { fixed: 'right', width: 300, align: 'center', toolbar: '#barDemo' }
                ]]
            });
            //监听头部工具栏
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id)
                    , data = checkStatus.data; //获取选中的数据
                switch (obj.event) {
                    case 'add':
                        layer.open({
                            type: 2,
                            area: ["40%", "60%"],
                            content: '/System/User_Add'
                        });

                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                var tr = obj.tr;
                if (obj.event === 'del') {
                    layer.confirm('真的删除吗', function (index) {
                        console.log(obj.data.Id)
                        $.post("/System/User_del", { id: obj.data.Id }, function (res) {
                            if (res.success) {
                                active["reload"].call(null);
                                layer.msg("成功", obj.othis);
                            }
                            else {
                                layer.msg("删除失败", obj.othis);
                            }
                        }, "json");
                    });
                } else if (obj.event === 'edit') {
                    var id = data['Id'];
                    layer.open({
                        type: 2,
                        title: "编辑用户信息",
                        area: ["40%", "65%"],
                        content: '/System/User_Edit?Id=' + id
                    });
                }
                else if (obj.event === 'RePwoard') {
                    layer.confirm("真的要重置密码吗？", function (index) {
                        console.log(obj.data.Id);
                        $.post("/System/User_RePoward", { id: obj.data.Id }, function (res) {
                            if (res.success) {
                                active["reload"].call(null);
                                layer.msg("成功", obj.othis);
                            }
                            else {
                                layer.msg("失败,请重试", obj.othis);

                            }

                        }, "json");
                    })
                }
                else if (obj.event === 'Search') {
                    //所有
                    $.post("GetRolesAll", function (result) {
                        var r = result.map(function (item) {
                            return {
                                value: item.Id,
                                title: item.RoleName
                            }
                        });
                        var rdata = new Array();
                        //当前
                        $.post("GetRolesByUserIdLayUI"
                            , { "Id": data.Id, }
                            , function (result) {
                                if (result.count > 0) {
                                    for (var i = 0; i < result.data.length; i++) {
                                        rdata.push(result.data[i].RoleId);
                                    }
                                }
                                //左右回调
                                transfer.render({
                                    elem: '#role_transfer',
                                    title: ['系统所有角色', '当前用户角色'],
                                    data: r,
                                    value: rdata,//右侧
                                    onchange: function (obj, index) {
                                        if (index == "0") {
                                            //设置用户角色
                                            for (var i = 0; i < obj.length; i++) {
                                                $.post("AddUserToRole", {
                                                    "Id": data.Id,
                                                    "rId": obj[i].value
                                                }, function (result) {

                                                }, "json");
                                            }

                                        } else {
                                            //删除用户角色
                                            for (var i = 0; i < obj.length; i++) {
                                                $.post("RemoveUserFromRole", {
                                                    "Id": data.Id,
                                                    "rId": obj[i].value
                                                }, function (result) {

                                                }, "json");
                                            }

                                        }
                                    }
                                });
                            }, "json");
                    }, "json");
                    //弹出角色设置框
                    layer.open({
                        type: 1,
                        title: '设置用户' + data.XingMin + '的角色',
                        content: $("#yh_role"),
                        area: ["60%", "85%"],
                        offset: '10px'
                    });
                }

            });
            active = {
                reload: function () {

                    //执行重载
                    table.reload('testReload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                        }
                    }, 'data');
                }
            };

            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });

    </script>
</body>
</html>
